"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 拷贝数据
Case Name   : stdin进行copy from
Create At   : 2022/7/11
Owner       : opentestcase002
Description :
    1.创建测试表
    drop table if exists t_copy_54;
    create table t_copy_54
    (sk integer,id varchar(16),name varchar(20),sq_ft integer);
    2.stdin进行copy from
    copy t_copy_54 from stdin;
    3.删除表
    drop table t_copy_54;
Expect      :
    1.创建测试表成功
    2.输入成功（将标准输入内容copy到表中）
    3.删除表成功
History     : 
"""
import unittest
import os

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant



class CopyFile(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.commonsh = CommonSH('PrimaryDbUser')
        self.userNode = Node('PrimaryDbUser')
        self.Constant = Constant()
        self.tb_name = 't_copy_54'

    def test_copy_file(self):
        text = '----step1:创建测试表;expect:创建成功---- '
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(
            f"drop table if exists {self.tb_name};"
            f"create table {self.tb_name} "
            f"(sk integer,id varchar(16),"
            f"name varchar(20),sq_ft integer);")
        self.log.info(sql_cmd)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, sql_cmd,
                      '执行失败:' + text)

        text = '----step2:进行标准输入：stdin进行copy frome;expect:输入成功----'
        self.log.info(text)
        copy_cmd = f'''source {macro.DB_ENV_PATH};
               expect <<EOF 
               spawn  gsql -p {self.userNode.db_port} \
               -d {self.userNode.db_name} -r \
               -c "copy {self.tb_name} from stdin;"
               expect ">>"
               send "1\\txx\\thh\\t22\\n"
               expect ">>"
               send "\\\\\.\\n"
               expect eof\n''' + '''EOF'''
        self.log.info(copy_cmd)
        msg = self.userNode.sh(copy_cmd).result()
        self.log.info(msg)
        self.log.info('----验证数据是否输入成功----')
        sql_cmd = self.commonsh.execut_db_sql(
            f"select * from {self.tb_name};")
        self.log.info(sql_cmd)
        self.assertIn('1 row', sql_cmd, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:删除表;expect:成功----'
        self.log.info(text)
        sql_cmd = self.commonsh.execut_db_sql(
            f"drop table {self.tb_name};")
        self.log.info(sql_cmd)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, sql_cmd)
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
